package com.tt.service.impl;

import com.tt.entity.bo.DepartmentBarBO;
import com.tt.entity.bo.IdNameBO;
import com.tt.entity.vo.DepartmentBarVO;
import com.tt.mapper.EnumDepartmentGeneralMapper;
import com.tt.mapper.EnumDepartmentSpecificMapper;
import com.tt.mapper.RawDepartmentMapper;
import com.tt.service.DepartmentBarService;
import com.tt.util.FuelEnum;
import com.tt.util.HandleDecimal;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * <p>
 *
 * </p>
 *
 * @version 1.0
 * @auther sugarttt
 * @since 2022-11-10 10:34:03
 */
@Service
public class DepartmentBarServiceImpl implements DepartmentBarService {

    private final RawDepartmentMapper rawDepartmentMapper;
    private final EnumDepartmentSpecificMapper enumDepartmentSpecificMapper;
    private final EnumDepartmentGeneralMapper enumDepartmentGeneralMapper;

    public DepartmentBarServiceImpl(RawDepartmentMapper rawDepartmentMapper, EnumDepartmentSpecificMapper enumDepartmentSpecificMapper, EnumDepartmentGeneralMapper enumDepartmentGeneralMapper) {
        this.rawDepartmentMapper = rawDepartmentMapper;
        this.enumDepartmentSpecificMapper = enumDepartmentSpecificMapper;
        this.enumDepartmentGeneralMapper = enumDepartmentGeneralMapper;
    }

    @Override
    public DepartmentBarVO getDepartmentBar(String beginYear, String endYear, Integer provinceId) {
        int diff = Integer.parseInt(endYear) - Integer.parseInt(beginYear) + 1;
        DepartmentBarVO departmentBarVO = new DepartmentBarVO();
        ArrayList<String> departmentNames = new ArrayList<>();
        ArrayList<String> fuelNames = new ArrayList<>();
        ArrayList<ArrayList<Double>> value = new ArrayList<>();

        int n = 17;
        while(n > 0) {
            value.add(new ArrayList<Double>());
            --n;
        }

        ArrayList<Integer> departmentCounts = new ArrayList<>();
        ArrayList<Integer> departmentIdS = new ArrayList<>();

        // 大部门id，name
        ArrayList<IdNameBO> generalDepartment = enumDepartmentGeneralMapper.getDepartmentIdName();
        Iterator<IdNameBO> departmentIterator = generalDepartment.iterator();



        // 大部门下对应的小部门数量
        IdNameBO idNameBO;
        while (departmentIterator.hasNext()) {
            idNameBO = departmentIterator.next();

            departmentIdS.add(idNameBO.getId());
            departmentCounts.add(enumDepartmentSpecificMapper.getDepartmentPId(idNameBO.getId()));
            departmentNames.add(idNameBO.getName());
        }




        // 为了排序，改变departmentCounts和departmentNames和departmentIdS中各部门位置为杂类(7)\交通(5)\制造(2)\电力(3)\采矿(1)\农林(0)\建筑(4)\批发(6)

        departmentCounts.add(4, departmentCounts.get(0));
        departmentCounts.add(4, departmentCounts.get(1));
        departmentCounts.remove(0);
        departmentCounts.remove(0);
        departmentCounts.add(0, departmentCounts.get(7));
        departmentCounts.add(0, departmentCounts.get(6));
        departmentCounts.remove(9);
        departmentCounts.remove(7);

        departmentNames.add(4, departmentNames.get(0));
        departmentNames.add(4, departmentNames.get(1));
        departmentNames.remove(0);
        departmentNames.remove(0);
        departmentNames.add(0, departmentNames.get(7));
        departmentNames.add(0, departmentNames.get(6));
        departmentNames.remove(9);
        departmentNames.remove(7);

        departmentIdS.add(4, departmentIdS.get(0));
        departmentIdS.add(4, departmentIdS.get(1));
        departmentIdS.remove(0);
        departmentIdS.remove(0);
        departmentIdS.add(0, departmentIdS.get(7));
        departmentIdS.add(0, departmentIdS.get(6));
        departmentIdS.remove(9);
        departmentIdS.remove(7);



        Iterator<Integer> departmentIdIterator = departmentIdS.iterator();

        DepartmentBarBO departmentBarBO;
        int index = 0;
        int m = diff * departmentCounts.get(index++);
        double[] sumValues = new double[20];
        while(departmentIdIterator.hasNext()) {
            ArrayList<DepartmentBarBO> departmentBarBOS = rawDepartmentMapper.getDepartmentBar(beginYear, endYear, provinceId, departmentIdIterator.next());
            Iterator<DepartmentBarBO> iterator = departmentBarBOS.iterator();

            for (int id = m; iterator.hasNext(); ) {
                departmentBarBO = iterator.next();

                if (id <= m) {
                    sumValues[0] += Double.parseDouble(departmentBarBO.getRawCoal());
                    sumValues[1] += Double.parseDouble(departmentBarBO.getCleanedCoal());
                    sumValues[2] += Double.parseDouble(departmentBarBO.getOtherWashedCoal());
                    sumValues[3] += Double.parseDouble(departmentBarBO.getBriquettes());
                    sumValues[4] += Double.parseDouble(departmentBarBO.getCoke());
                    sumValues[5] += Double.parseDouble(departmentBarBO.getCokeOvenGas());
                    sumValues[6] += Double.parseDouble(departmentBarBO.getOtherGas());
                    sumValues[7] += Double.parseDouble(departmentBarBO.getOtherCokingProducts());
                    sumValues[8] += Double.parseDouble(departmentBarBO.getCrudeOil());
                    sumValues[9] += Double.parseDouble(departmentBarBO.getGasoline());
                    sumValues[10] += Double.parseDouble(departmentBarBO.getKerosene());
                    sumValues[11] += Double.parseDouble(departmentBarBO.getDieselOil());
                    sumValues[12] += Double.parseDouble(departmentBarBO.getFuelOil());
                    sumValues[13] += Double.parseDouble(departmentBarBO.getLpg());
                    sumValues[14] += Double.parseDouble(departmentBarBO.getRefineryGas());
                    sumValues[15] += Double.parseDouble(departmentBarBO.getOtherPetroleumProducts());
                    sumValues[16] += Double.parseDouble(departmentBarBO.getNaturalGas());
                }

                --id;

                if (id == 0) {
                    value.get(0).add(HandleDecimal.handle(sumValues[0]));
                    value.get(1).add(HandleDecimal.handle(sumValues[1]));
                    value.get(2).add(HandleDecimal.handle(sumValues[2]));
                    value.get(3).add(HandleDecimal.handle(sumValues[3]));
                    value.get(4).add(HandleDecimal.handle(sumValues[4]));
                    value.get(5).add(HandleDecimal.handle(sumValues[5]));
                    value.get(6).add(HandleDecimal.handle(sumValues[6]));
                    value.get(7).add(HandleDecimal.handle(sumValues[7]));
                    value.get(8).add(HandleDecimal.handle(sumValues[8]));
                    value.get(9).add(HandleDecimal.handle(sumValues[9]));
                    value.get(10).add(HandleDecimal.handle(sumValues[10]));
                    value.get(11).add(HandleDecimal.handle(sumValues[11]));
                    value.get(12).add(HandleDecimal.handle(sumValues[12]));
                    value.get(13).add(HandleDecimal.handle(sumValues[13]));
                    value.get(14).add(HandleDecimal.handle(sumValues[14]));
                    value.get(15).add(HandleDecimal.handle(sumValues[15]));
                    value.get(16).add(HandleDecimal.handle(sumValues[16]));
                    sumValues = new double[20];
                    if (index < departmentCounts.size()) {
                        id = m = diff * departmentCounts.get(index++);
                    }
                }
            }
        }


        fuelNames.add(FuelEnum.RawCoal.getZhName());
        fuelNames.add(FuelEnum.CleanedCoal.getZhName());
        fuelNames.add(FuelEnum.OtherWashedCoal.getZhName());
        fuelNames.add(FuelEnum.Briquettes.getZhName());
        fuelNames.add(FuelEnum.Coke.getZhName());
        fuelNames.add(FuelEnum.CokeOvenGas.getZhName());
        fuelNames.add(FuelEnum.OtherGas.getZhName());
        fuelNames.add(FuelEnum.OtherCokingProducts.getZhName());
        fuelNames.add(FuelEnum.CrudeOil.getZhName());
        fuelNames.add(FuelEnum.Gasoline.getZhName());
        fuelNames.add(FuelEnum.Kerosene.getZhName());
        fuelNames.add(FuelEnum.DieselOil.getZhName());
        fuelNames.add(FuelEnum.FuelOil.getZhName());
        fuelNames.add(FuelEnum.LPG.getZhName());
        fuelNames.add(FuelEnum.RefineryGas.getZhName());
        fuelNames.add(FuelEnum.OtherPetroleumProducts.getZhName());
        fuelNames.add(FuelEnum.NaturalGas.getZhName());


        departmentBarVO.setDepartmentName(departmentNames);
        departmentBarVO.setFuelName(fuelNames);
        departmentBarVO.setValue(value);
        return departmentBarVO;
    }
}
